<html>
<head><meta charset="utf-8"><title>Identify generic parameters across multiple impls · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Identify.20generic.20parameters.20across.20multiple.20impls.html">Identify generic parameters across multiple impls</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224655134"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Identify%20generic%20parameters%20across%20multiple%20impls/near/224655134" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JOE1994 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Identify.20generic.20parameters.20across.20multiple.20impls.html#224655134">(Jan 31 2021 at 17:30)</a>:</h4>
<p>Hello <span aria-label="crab" class="emoji emoji-1f980" role="img" title="crab">:crab:</span> ,<br>
I'm trying to inspect the usage of a struct's generic parameters across multiple <code>impl</code>s or methods.</p>
<p>It seems that both the index (<code>rustc_middle::ty::GenericParamDef.index : u32</code>) and the <code>DefId</code>of a generic parameter (<code>rustc_middle::ty::GenericParamDef.def_id</code>) are only unique within a given <code>impl</code> or method and are not globally unique.</p>
<p><strong>Below shows an example where the index/<code>DefId</code> of <code>B</code> from <code>My&lt;A, B&gt;</code> differ across different <code>impl</code>s <span aria-label="sad" class="emoji emoji-2639" role="img" title="sad">:sad:</span> </strong></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">My</span><span class="o">&lt;</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">val1</span>: <span class="nc">A</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">val2</span>: <span class="nc">B</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// F.index = 1</span>
<span class="c1">// F.DefId = "DefId(0:10 ~ identify_generic_params[8787]::{{impl}}[0]::F[0])"</span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">impl</span><span class="o">&lt;'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="n">F</span>: <span class="nb">Send</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Send</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">My</span><span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="o">&gt;</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"> </span><span class="n">F</span>: <span class="nb">Fn</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="kt">u32</span> <span class="p">{}</span><span class="w"></span>

<span class="c1">// F.index = 0</span>
<span class="c1">// F.DefId = "DefId(0:12 ~ identify_generic_params[8787]::{{impl}}[1]::F[0])"</span>
<span class="k">unsafe</span><span class="w"> </span><span class="k">impl</span><span class="o">&lt;</span><span class="n">F</span>: <span class="nb">Sync</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Sync</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">My</span><span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="o">&gt;</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"> </span><span class="n">F</span>: <span class="nb">Fn</span><span class="p">(</span><span class="o">&amp;</span><span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="kt">u32</span> <span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>Is there a canonical way to identify same generic parameters across multiple <code>impl</code>s/methods?<br>
Or is this a feature that isn't yet readily available?</p>
<p>Thank you for checking out this post!</p>



<a name="224655370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Identify%20generic%20parameters%20across%20multiple%20impls/near/224655370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Identify.20generic.20parameters.20across.20multiple.20impls.html#224655370">(Jan 31 2021 at 17:37)</a>:</h4>
<p>you can look at the self type of the impl</p>



<a name="224655376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Identify%20generic%20parameters%20across%20multiple%20impls/near/224655376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Identify.20generic.20parameters.20across.20multiple.20impls.html#224655376">(Jan 31 2021 at 17:37)</a>:</h4>
<p>which should be <code>ty::Adt(def_id, substs)</code> and afaict <code>substs</code> is what you are looking for</p>



<a name="224656494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Identify%20generic%20parameters%20across%20multiple%20impls/near/224656494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JOE1994 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Identify.20generic.20parameters.20across.20multiple.20impls.html#224656494">(Jan 31 2021 at 18:02)</a>:</h4>
<p>Using <code>substs</code> seems to be the way! Thank you very much <span aria-label="happy" class="emoji emoji-1f600" role="img" title="happy">:happy:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>